<?php

namespace App\Service;


use App\User;
use App\Repositories\Permission as PermissionRepository;
use Illuminate\Http\Request;

class Permission
{
    private $request;

    //
    /**
     * 获取用户权限
     * @param $userId
     *
     * @return array
     */
    public static function getAllPermission($userId){
        if(!(int)$userId>0) return [];
        $user = User::find($userId);
        if(!$user) return [];
        $allpemissions = $user->getAllPermissions()->pluck('name');
        if(!$allpemissions) return [];
        return $allpemissions->toArray();
    }
    /**
     * @param Request $request
     *
     * @return $this|\Illuminate\Database\Eloquent\Builder|static
     */
    public function getPageList(Request $request){
        $this->request = $request;
        $model = new PermissionRepository();
        if(isset($request->group_name)&&$request->group_name){
            $model = $model->where('group_name',$request->group_name);
        }
        if(isset($request->guard_name)&&$request->guard_name){
            $model = $model->where('guard_name',$request->guard_name);
        }
        $model=$model->paginate($this->request->limit)
            ->toArray();
        return $model;
    }
    public static function getGroups(){
        return PermissionRepository::groupBy('group_name')->pluck('group_name')->toArray();
    }
    public static function getGuardAry(){
        $permissionRepository =  new PermissionRepository();
        return $permissionRepository->getGuardAry();
    }
    /**
     * @param array $id
     * @param int $sort
     */
    public function sortCol($id = [], $sort = 50){
        if($id == []){
        }else{
            if(is_array($id)){
                PermissionRepository::whereIn('id',$id)->where('order','<>',$sort)->update(['order'=>$sort]);
            }else{
                PermissionRepository::where('id',$id)->where('order','<>',$sort)->update(['order'=>$sort]);

            }
        }
    }

    /**
     * @param array $id
     */
    public function del($id = []){
        if($id == []){

        }else{
            if(is_array($id)){
                PermissionRepository::whereIn('id',$id)->delete();
            }else{
                PermissionRepository::where('id',$id)->delete();
            }
        }
    }
    /**
     * @param $post
     */
    public function insertOrUpdate($post){
        if(isset($post['id'])){
            $model = PermissionRepository::find($post['id']);
        }else{
            $model = new PermissionRepository();
        }
        $model->name = $post['name'];
        $model->guard_name = $post['guard_name'];
        $model->desc = $post['desc'];
        $model->group_name = $post['group_name'];
        $model->save();
    }
    public static function __callStatic ($name, $arguments)
    {
        // TODO: Implement __callStatic() method.
        if(starts_with($name,'findBy')){
            $tar = explode('findBy',$name);
            if(count($tar)>1){
                return PermissionRepository::where(lcfirst($tar[1]),$arguments)->first();
            }else{
                throw new \Exception('方法不存在');
            }
        }elseif (starts_with($name,'getBy')){
            $tar = explode('getBy',$name);
            if(count($tar)>1){
                return PermissionRepository::where(lcfirst($tar[1]),$arguments)->get();
            }else{
                throw new \Exception('方法不存在');
            }
        }
    }
}
